<? if (!defined('DB_PEREFIX')) exit;


global $db, $template;

if (isset($_GET['page']) && $_GET['page'] != '0') 
	$start = (intval ($_GET['page']) - 1) * 30;
else 
	$start = 0;
	
load_helper('page');


past_navblock (array (
					array ('Написать статью', '/blog/edit/add/', 'cat', 'add'),
					array ('Поиск по тегам', '/blog/tags/', 'cat', 'tags'),
					array ('Комментарии', '/blog/comments/', 'name', 'art_comments'),
					array ('TOP блогов', '/blog/top/', 'name', 'top'),
					)
			);

$query = $db->query ("SELECT com_id, com_uid, com_idi, com_author, com_mail, com_text, com_date, user_ip, article.art_header, article.art_category, category.cat_header  FROM ".DB_PEREFIX."_comments AS comment
	LEFT JOIN (SELECT art_id, art_header, art_category FROM ".DB_PEREFIX."_articles) AS article ON comment.com_idi = article.art_id
	LEFT JOIN (SELECT cat_name, cat_header FROM ".DB_PEREFIX."_art_category) AS category ON category.cat_name = article.art_category
	ORDER BY com_id DESC LIMIT $start, 30");
	
	//WHERE com_category = 'articles' 

// FIXME: Маразм
// TODO: Добавить связь с таблице пользователей по uid
$count = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_comments;"));
echo '<a name="comments"></a><div id="comments_div"><br><br>';
	
	while ($com = $db->fetch_array ($query)) 
	{
		display_comment_ (	$com['com_id'], $com['com_uid'], $com['com_author'], 
							$com['com_mail'], $com['com_text'], $com['com_date'],
							$com['user_ip'], $com['com_idi'], $com['art_header'], 
							$com['art_category'], $com['cat_header']);
	}	
		
echo '</div>';
			
if ($count > 30) print past_pages($start, 30, $count, '/blog/comments/');


function display_comment_ ($id, $uid, $author, $mail, $text, $date, $ip, $idi, $art_header, $category, $cat_header) 
{
	global $SITE, $template, $GLOBAL_USER, $db;
	$author = stripslashes($author);
	$text = stripslashes($text);
	$img = '';
	
	if ($mail == '') $name = $author;
	if ($mail != '') 
	{
		$mail = str_replace ('mailto:', '', $mail);
		$name = '<a href="mailto:'.$mail.'">'.$author.'</a>';
	}

	$edit = '';
	
	if (is_admin()) 
	{
		$edit  = '<img src="images/spacer.gif" width="20" height="1">';
		$edit .= '<a href="/" onclick="delete_comments('.$id.', "'.$module.'",'.$idi.'); return false;">';
		$edit .= '<img src="/images/cut.gif" border="0" title="Удалить" align="absmiddle"></a>"';
		
		$edit .= '<a href="javascript:void(0);" onclick="edit_com('.$id.');">';
		$edit .= '<img src="/images/edit.gif" border="0" title="Редактировать" align="absmiddle"></a>';
		$edit .= '<b>IP:</b> <a href="http://www.nic.ru/whois/?ip='.$ip.'" title="Информация об '.$ip.'" target="_blank">'.$ip.'</a>';
	}

	if ($uid != "" && $uid != 0) 
	{
		$user = $db->fetch_array ($db->query ("SELECT * FROM ".DB_PEREFIX."_users WHERE user_id = '$uid' LIMIT 1;"));
		$uname = $user['user_name'];
		$name = '<a href="/user/'.$uid.'.html">'.$uname.'</a>';

		if ($user['user_avatar'] != '') 
			$avatar = $user['user_avatar'];
		else 
			$avatar = "noavatar.gif";

		// Если аватар не задан то устанавливаем стандартный
		if ($user['user_avatar'] != '') 
			$avatar = '/uploads/avatars/s'.$uid.'.jpg';
		else 
			$avatar = '/images/u_snoavatar.gif';			

		$img = '<div class="avatar"><a href="/user/'.$uid.'.html"><img src="'.$avatar.'" title="Аватар для '.$uname.'" /></a></div>';
	}

	if ($uid != '' && $uid != 0) 
	{
		//$template->set_var ("{USER_STATUS}", user_status($uid));
		$template->set_var ('{USER_ID}', $uid);
	}
	else 
	{
		//$template->set_var ("{USER_STATUS}", "");
		$template->set_var ('{USER_ID}', '');
	}
	
	$template->set_var ('{NAME}', $name);
	$template->set_var ('{ART_HEADER}', $art_header);
	$template->set_var ('{CAT_HEADER}', $cat_header);
	$template->set_var ('{CATEGORY}', $category);
	$template->set_var ('{COMMENT_ID}', $id);
	$template->set_var ('{DATE}', format_time($date));
	$template->set_var ('{EDIT}', $edit);
	$template->set_var ('{AVATAR}', $img);
	$template->set_var ('{BODY}', n2br($text));
	$template->set_var ('{BOTTOM}', $bottom);
	$template->set_var ('{IDI}', $idi);
	print $template->display ('blog','advanced_comment.tpl');
}

